<?php


class assets_model extends Model
{
    function get_search_result($keyword, $tags, $page_size, $order, $asset_ids = array(), $search_type = 0, $status = 'is_publish = 2')
    {
        $order_asc_desc = explode('|', $order);
        $order = $order_asc_desc[0];
        $adesc = $order_asc_desc[1];
        $order = ' is_recommend DESC, '.$order.' '.$adesc;
        if (empty($keyword) && empty($tags)) {
            
            $sqladd = '';
            if ($asset_ids) {
                $sqladd = "AND id IN(".implode(',', $asset_ids).")";
            }
            $sql = "WHERE $status $sqladd ORDER BY $order ";
            return $this->getList($sql, new Pager($page_size));
        } elseif (empty($tags)) {
            $keyword = addslashes($keyword);
            $sqladd = '';
            if ($asset_ids) {
                $sqladd = "AND id IN(".implode(',', $asset_ids).")";
            }
            if ($search_type) {
                return $this->getList("WHERE (user_id = (SELECT id FROM user WHERE full_name = '$keyword') ) AND $status $sqladd ORDER BY $order ", new Pager($page_size));
            } else {
                return $this->getList("WHERE (title LIKE '%$keyword%' OR FIND_IN_SET('$keyword', tags)) AND $status $sqladd ORDER BY $order ", new Pager($page_size));
            }
        } else {
            if ($asset_ids) {
                $sqladd = "AND asset_id IN(".implode(',', $asset_ids).")";
            }
            // $sql = "SELECT * FROM assets_tag WHERE tag_id IN(".implode(',', $tags).") $sqladd";
            
            $sql = "SELECT *, group_concat(tag_id) as tags FROM assets_tag WHERE tag_id IN(".implode(',', $tags).") $sqladd GROUP BY asset_id";
            $data = $this->db->getAll($sql);
            
            
            $sql = "SELECT *, group_concat(id) as tags FROM tags WHERE parent_id != 0  GROUP BY parent_id";
            $tag_group = $this->db->getAll($sql);
            foreach ($tag_group as $key => &$value) {
                $value['tags'] = explode(',', $value['tags']);
            }
            foreach ($data as $key => &$value) {
                $value['tags'] = explode(',', $value['tags']);
            }
            $tags_g = array();
            foreach ($tag_group as $key => $value) {
                $tags_g_t = array_intersect($value['tags'],$tags);
                if (!empty($tags_g_t)) {
                     $tags_g[] = $tags_g_t;
                }
            }
            $tags_g = array_filter($tags_g);
            foreach ($data as $k=>$val) {
                foreach ($tags_g as $kk) {
                    $tmp_aa = array_intersect($val['tags'],$kk);
                    if (empty($tmp_aa)) {
                        unset($data[$k]);
                    }
                }
            }
            
            // $t = array();
            // foreach ($data as $key => $val) {
            //     if (isset($t[$val['asset_id']])) {
            //         $t[$val['asset_id']]++;
            //     } else {
            //         $t[$val['asset_id']] = 1;
            //     }
            // }
            // $asset_ids = array_keys($t, count($tags));
            // if (empty($asset_ids)) {
            //     return array('data'=>array());
            // }
            $asset_ids = array();
            foreach ($data as $val) {
                $asset_ids[] = $val['asset_id'];
            }
            $asset_ids = array_filter($asset_ids);
            if (empty($asset_ids)) {
                return array('data'=>array());
            }
            $sql = 'WHERE $status AND id IN('.implode(',', $asset_ids).')';
            if ($keyword) {
                $keyword = addslashes($keyword);
                if ($search_type) {
                    $sql .= " AND (user_id = (SELECT id FROM user WHERE full_name = '$keyword'))";
                } else {
                    $sql .= " AND (title LIKE '%$keyword%' OR FIND_IN_SET('$keyword', tags))";
                }
            }
            $sql .= " ORDER BY $order ";
            return $this->getList($sql, new Pager($page_size));
        }
    }

    function get_one_version($asset_id)
    {
        if (!$version = _model('assets_version')->read(array('asset_id'=>$asset_id), 'ORDER BY view_order ASC')) {
            return array();
        }
        $version['files'] = _model('assets_file')->getList(array('version_id'=>$version['id']), 'ORDER BY add_time DESC');

        return $version;
    }

    function download_history($user_id, $limit = '')
    {
        $sql = "SELECT DISTINCT asset_id FROM download_history WHERE user_id = $user_id AND asset_id NOT IN(SELECT asset_id FROM vote_history WHERE user_id = $user_id) ORDER BY update_time DESC $limit";

        return $this->db->getAll($sql);
    }

    function get_relate($asset_id)
    {
        $sql = "SELECT a.* FROM assets a LEFT JOIN assets_relate r ON r.relate_asset_id = a.id WHERE r.asset_id = ? ORDER BY add_time DESC";
        
        return $this->db->getAll($sql, $asset_id);
    }

    function get_info($id)
    {
        if (!$info = $this->read(array('id'=>$id))) {
            return array();
        }
        $sql = "SELECT t.* FROM assets_tag a LEFT JOIN tags t ON a.tag_id = t.id WHERE a.asset_id = ? ";
        $tags = array();
        $data = $this->db->getAll($sql, $id);
        foreach ($data as $key => $val) {
            $tags[$val['id']] = $val;
        }
        
        $p_tag = _model('tags')->getList(array('parent_id'=>0));
        foreach ($p_tag as $key => $val) {
            $val['tags'] = array();
            foreach ($tags as $k => $v) {
                if ($v['parent_id'] == $val['id']) {
                    $val['tags'][] = $v;
                }
            }
            if (empty($val['tags'])) {
                unset($p_tag[$key]);
                continue;
            }
            $p_tag[$key] = $val;
        }

        $info['set_tags'] = $p_tag;
        if ($info['append']) {
            $info['append'] = unserialize($info['append']);
        } else {
            $info['append'] = array();
        }

        if ($info['tags']) {
            $info['tags'] = array_filter(explode(',', $info['tags']));
        } else {
            $info['tags'] = array();
        }

        return $info;
    }
}


?>